package mytest;

import java.util.concurrent.atomic.*;

public class CasSingleton {

    private CasSingleton(){}

    private static AtomicReference<CasSingleton> instance = new AtomicReference<>();

    public static CasSingleton getInstance(){
        while (true){
            CasSingleton casSingleton = instance.get();
            if(null != casSingleton){
                return casSingleton;
            }
            casSingleton = new CasSingleton();

            if(instance.compareAndSet(null, casSingleton)) {
                return casSingleton;
            }
        }
    }

    // 变量类
    private AtomicInteger atomicInteger;
    private AtomicLong atomicLong;
    private AtomicBoolean atomicBoolean;
    private AtomicReference<CasSingleton> atomicReference;

    // 更新器类
    private AtomicLongFieldUpdater<CasSingleton> atomicLongFieldUpdater;
    private AtomicIntegerFieldUpdater<CasSingleton> atomicIntegerFieldUpdater;
    private AtomicReferenceFieldUpdater<CasSingleton, String> atomicReferenceFieldUpdater;

    // 数组类
    private AtomicIntegerArray atomicIntegerArray;
    private AtomicLongArray atomicLongArray;
    private AtomicReferenceArray<CasSingleton> atomicReferenceArray;

    // 复合变量类
    private AtomicMarkableReference<CasSingleton> atomicMarkableReference;
    private AtomicStampedReference<CasSingleton> atomicStampedReference;

}
